
import axios from "axios";
import { mainStore } from "../stores"
import {baseUrl} from "@/utils/util"

const config = {
    // baseURL: "https://61211w73f7.zicp.fun",
    baseURL:baseUrl(),
    timeout: 3000,

}

class RequestHttp {
    service = null
    constructor(config) {
        this.service = axios.create(config)

        /**
       * @description 请求拦截器
       * 客户端发送请求 -> [请求拦截器] -> 服务器
       * token校验(JWT) : 接受服务器返回的token,存储到vuex/pinia/本地储存当中
       */

        this.service.interceptors.request.use((config) => {
            const mStore = mainStore()
            const token = mStore.token

            return { ...config, headers: { ...config.headers, "x-access-token": token } }
        }, (err) => {
            return Promise.reject(err)
        })

        /**
      * @description 响应拦截器
      *  服务器换返回信息 -> [拦截统一处理] -> 客户端JS获取到信息
      */

        this.service.interceptors.response.use((res) => {
            const { data } = res
            return data
        }, (err) => {
            return Promise.reject(err)
        })
    }

    get(url, params = {}) {
        return this.service.request({
            url,
            params,
            method: "get",
            headers: { "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" }
        })
    }

    videoPost(url, params = {}) {
        return this.service.request({
            url,
            params,
            method: "post",
            headers: {
                // "Content-Type": "multipart/form-data; boundary='56423498738365'"
                "Content-Type": "multipart/form-data;boundary=56423498738365"
            }
        })
    }

    post(url, params = {}) {
        return this.service.post(url, params,)
    }


}

export default new RequestHttp(config)